<?php
class ForgotForm extends CFormModel
{
    public $username_email;
    public $verifyCode;
    
    public function rules()
    {
        return array(
            array('username_email', 'required'),
            array('username_email', 'checkExist'), 
            array('verifyCode', 'CaptchaExtendedValidator', 'allowEmpty'=>!CCaptcha::checkRequirements()),  
        );
    }

    public function attributeLabels()
    {
        return array(
            'username_email'=> Yii::t('user', 'Username/Email'),
            'verifyCode'    =>  Yii::t('user', 'Captcha'),
        );
    }

    public function checkExist($attribute,$params)
    {
        $is_email = preg_match('{@}', $this->username_email);
        if($is_email){
            $user = User::model()->findByAttributes(array('email' => $this->username_email));
        }else{
            $user = User::model()->findByAttributes(array('username' => $this->username_email));
        }

        if(!$user){
            if($is_email){
                $this->addError('username_email', Yii::t('user', 'Not exist user with email {username_email}.', array('{username_email}' => $this->username_email)));
            }else{
                $this->addError('username_email', Yii::t('user', 'Not exist user with username {username_email}.', array('{username_email}' => $this->username_email)));
            }
        }    
    }

}
